package com.atguigu.tingshu.user.api;

import com.atguigu.tingshu.common.login.annotation.TingshuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Tag(name = "微信授权登录接口")
@RestController
@RequestMapping("/api/user/wxLogin")
@Slf4j
public class WxLoginApiController {

    @Autowired
    private UserInfoService userInfoService;

    @GetMapping("/wxLogin/{code}")
    @Parameters({
            @Parameter(name = "code", description = "微信授权码", required = true)
    })
    @Operation(summary = "微信小程序登录")
    public Result<HashMap<String, Object>> wxLogin(@PathVariable(value = "code") String code) {
        log.info("微信授权登录，code：{}", code);
        HashMap<String, Object> map = userInfoService.wxLogin(code);
        return Result.ok(map);
    }
    // 前端不会来调用（未来需要修改一下前端代码逻辑） 后端提前提供好了。
    @GetMapping("/refreshToken")
    @Operation(summary = "刷新Token接口")
    public Result<HashMap<String, Object>> refreshToken() {
        HashMap<String, Object> map = userInfoService.refreshToken();
        return Result.ok(map);
    }


//    private final Long userId = AuthContextHolder.getUserId();

    @GetMapping("/getUserInfo")
    @Operation(summary = "获取登录用户信息")
    @TingshuLogin
    public Result getUserInfo() {

        Long userId = AuthContextHolder.getUserId();

        UserInfoVo userInfoVo = userInfoService.getUserInfo(userId);

        return Result.ok(userInfoVo);
    }

    @Operation(summary = "更新用户信息")
    @PostMapping("/updateUser")
    public Result<Boolean> updateUser(@RequestBody UserInfoVo userInfoVo) {
        Boolean flag = userInfoService.updateUser(userInfoVo);
        return Result.ok(flag);
    }



}
